package com.yym.cloudweb.crawl.station.jd;

import com.google.common.collect.Lists;
import com.yym.cloudweb.common.Station;
import com.yym.cloudweb.crawl.util.CrawlUtil;
import com.yym.cloudweb.entity.Good;
import com.yym.cloudweb.mapper.GoodMapper;
import com.yym.cloudweb.util.SpringContextHolder;
import org.jsoup.nodes.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.io.IOException;
import java.util.List;

@Service
public class JdGpuPageProcessor  implements PageProcessor {


    private static Logger LOGGER = LoggerFactory.getLogger(JdGpuPageProcessor.class);

    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000).setTimeOut(10000);

    @Override
    public void process(Page page) {
        try {
            dealPageData(page);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Site getSite() {
        return site;
    }

    public void dealPageData(Page page) throws IOException {
        List<Object> goodList = CrawlUtil.getElements(page.getHtml().toString(),"//div[@id='J_goodsList']/ul/li");
        List<Good> dataList = Lists.newArrayList();
        for(Object ele : goodList){
            if(ele instanceof Element){
                Element element = (Element)ele;
                Object pic = CrawlUtil.getValByXpath(element,"//div[@class='p-img']/a/img/@src");
                Object price = CrawlUtil.getValByXpath(element,"//div[@class='p-price']/strong/i/text()");
                Object goodLink = CrawlUtil.getValByXpath(element,"//div[@class='p-name p-name-type-2']/a/@href");
                Object name = CrawlUtil.getValByXpath(element,"//div[@class='p-name p-name-type-2']/a/em/text()");
                Good good = new Good();
                if(price != null){
                    good.setPrice(Double.parseDouble(price.toString()));
                }
                if(goodLink != null){
                    good.setLink(goodLink.toString());
                }
                if(name != null){
                    good.setGoodName(name.toString());
                }
                if(pic != null){
                    good.setPic(pic.toString());
                }
                good.setSource(Station.JD);
                dataList.add(good);
            }
        }
        page.putField("goodList",dataList);
    }

}
